<?php
namespace Blog\BlogBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\HttpFoundation\Request;
use Blog\BlogBundle\Form\BlogPostType;
use Blog\BlogBundle\Form\BlogCommentType;
use Doctrine\Common\Persistence\ObjectManager;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of blogController
 *
 * @author bien.tran
 */
class BlogController extends Controller {
    public function indexAction() {
        return $this->render('BlogBundle::layout.html.twig');
    }
    public function addNewBlogAction(Request $request) {
        $blogPost = $this->get('blogpost');
        $form = $this->createForm(new BlogPostType(), $blogPost);
        if($request->getMethod() == 'POST')
        {
            $form = $this->createForm(new BlogPostType(), $blogPost);
            $form->bindRequest($request);
            if($form->isValid())
            {
                $blogPost->setCreatedAt(new \DateTime());
                $blogInsert = $this->getDoctrine()->getEntityManager();
                $blogInsert->persist($blogPost);
                $blogInsert->flush();
                return $this->redirect($this->generateUrl('BlogBundle_viewBlog', array('id' => $blogPost->getId())));
            }
            else
                return $this->render('BlogBundle:blog:newblog.html.twig', array('form' => $form->createView(), 'task' => 'Please input information to your blog'));
        }
        return $this->render('BlogBundle:blog:newblog.html.twig', array('form' => $form->createView()));
    }
    public function viewBlogAction($id) {
        $blogView = $this->getDoctrine()->getEntityManager();
        $blogComment = $this->get('blogcomment');
        $formComment = $this->createForm(new BlogCommentType(), $blogComment);
        $viewBlog = $blogView->find('BlogBundle:BlogPost', $id);

        $blogComments = $blogView->getRepository('BlogBundle:BlogComment')->createQueryBuilder('bc')
                ->where('bc.post = '. $id)
                ->orderBy('bc.createdAt', 'DESC')
                ->getQuery()
                ->getResult();
        
        return $this->render('BlogBundle:blog:viewblog.html.twig', array('viewBlog' => $viewBlog, 'formComment' => $formComment->createView(), 'blogComments' => $blogComments));
    }

    public function postCommentAction(Request $request) {
        $blogComment = $this->get('blogcomment');
        if ($request->getMethod() == 'POST') {
            
            $blogId = $request->get('blogId', 0);
            $form = $this->createForm(new BlogCommentType(), $blogComment);
            $form->bindRequest($request);
            
            if ($form->isValid()) {
                $result = $this->getDoctrine()->getEntityManager();

                $blogComment->setAuthor(1);
                $blogComment->setPost($result->find('BlogBundle:BlogPost', $blogId));
                $blogComment->setCreatedAt(new \DateTime());

                $result->persist($blogComment);
                $result->flush();
                $this->get('session')->setFlash('statusAdd', 'Send comment successful!');
            }
            else
            {
                $this->get('session')->setFlash('statusAdd', 'Send comment faild!');
            }
            return $this->redirect($this->generateUrl('BlogBundle_viewBlog', array('id' => $blogId)));
        }
        return $this->redirect($this->generateUrl('BlogBundle_viewBlog', array('id' => 1)));
    }

}

?>
